<?php

/**
 * A PHP reader of Aborior's Simplex database
 *
 * PHP version 5
 *
 * Simplex is now a defunct database format, so this package only supports
 * reading of database to recover data.
 *
 * @category  Database
 * @package   DB_Simplex
 * @author    Philippe Jausions <jausions@php.net>
 * @copyright 2008 by 11abacus / Philippe Jausions
 * @license   http://www.opensources.org/licenses/bsd-license.php BSD license
 * @version   CVS: $Id: $
 * @link      http://pear.11abacus.com/package/DB_Simplex
 */

require_once 'MDB2/Driver/Datatype/Common.php';

/**
 * MDB2 Aborior's Simplex driver
 *
 * @category  Database
 * @package   DB_Simplex
 * @author    Philippe Jausions <jausions@php.net>
 * @copyright 2008 by 11abacus / Philippe Jausions
 * @license   http://www.opensources.org/licenses/bsd-license.php BSD license
 * @link      http://pear.11abacus.com/package/DB_Simplex
 */
class MDB2_Driver_Datatype_simplex extends MDB2_Driver_Datatype_Common
{
    /**
     * Maps a native array description of a field to a MDB2 datatype and length
     *
     * @param array $field native field description
     * 
     * @return array containing the various possible types, length, sign, fixed
     */
    public function _mapNativeDatatype($field)
    {
        $type   = array();
        $fixed  = null;
        $length = $field['length']; 
        switch (strtolower($field['type'])) {
        case 'autointeger (seq)':
            $type[] = 'integer';
            break;
        case 'number':
        case 'integer':
        case 'counter':
            /**
             * @todo Determine the length in bytes, or whether to be float?
             */
            $type[] = 'integer';
            break;
        case 'ip address':
            /**
             * @todo Check proper conversion?
             */
            $type[] = 'integer';
            $type[] = 'unsigned';
            $length = 4;
            break;
        case 'text':
        case 'url':
        case 'email':
        case 'password':
            $type[] = 'text';
            break;
        case 'true/false':
            $type[] = 'boolean';
            break;
        case 'date':
            $type[] = 'date';
            $length = null;
            break;
        case 'time':
            $type[] = 'time';
            $length = null;
            break;
        case 'file':
        case 'memo':
            $type[] = 'blob';
            $length = null;
            break;
        /**
         * @todo Determine mapping for other types
         */
        case 'handle':
        case 'autointeger (ran)':
        default:
            $db =& $this->getDBInstance();
            if (PEAR::isError($db)) {
                return $db;
            }

            return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
                'unknown database attribute type: '.$db_type, __FUNCTION__);
        }

        if ((int)$length <= 0) {
            $length = null;
        }

        return array($type, $length, null, null);
    }
}

?>